using System.Drawing;
using System.Windows.Forms;
using DrawTools.Tools;

namespace DrawTools.Editor.Tools.States
{
  public class CropSelectionState : IPointerToolState
  {
    private readonly IDrawingCanvas _canvas;
    private readonly int _cropHandle;
    private Point _lastPoint;

    public CropSelectionState(IDrawingCanvas canvas, int cropHandle, Point lastPoint)
    {
      _canvas = canvas;
      _cropHandle = cropHandle;
      _lastPoint = lastPoint;
    }

    public void Update(Point point)
    {
      if (_cropHandle > 0)
      {
        bool shiftPressed = (Control.ModifierKeys & Keys.Shift) != 0;
        _canvas.Crop.MoveHandleTo(point, _cropHandle, shiftPressed);
      }
      else
      {
        int dx = point.X - _lastPoint.X;
        int dy = point.Y - _lastPoint.Y;

        _lastPoint.X = point.X;
        _lastPoint.Y = point.Y;

        _canvas.Crop.Move(dx, dy);
        _canvas.SetCursor(Cursors.SizeAll);
      }

      _canvas.Refresh();
    }

    public void Release()
    {
      _lastPoint = new Point(0, 0);
    }
  }
}